import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.launcher import Launcher
from resource.pom.lock_screen import LockScreen
from resource.pom.settings import Settings


class Test:
    app = Settings
    keyboard = Keyboard
    lock_screen = LockScreen

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1.点击设置-生物识别和密码-锁屏密码，点击〇框，输入6位数字')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.biometrics_password_item)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('锁屏密码'))
        device.refresh_layout()
        device.click_element(device.get_element_by_type('List'))
        logging.info('预期结果1.正常唤醒键盘，可输入6位PIN数字密码，如果输入的不是数字，则有“PIN应为6位数字”的提示信息')
        device.wait(1)
        device.assert_soft_keyboard_on()
        device.click(*self.keyboard.white_space)
        device.refresh_layout()
        device.assert_text_exist('PIN 应为 6 位数字')
        device.click(*self.keyboard.delete)
        device.click(*self.keyboard.abc_123)
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.click(*self.keyboard.key_4)
        device.click(*self.keyboard.key_5)
        device.click(*self.keyboard.key_6)

        logging.info('步骤2.再次输入相同的6位数字')
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.click(*self.keyboard.key_4)
        device.click(*self.keyboard.key_5)
        device.click(*self.keyboard.key_6)
        logging.info('预期结果2.输入完成后会自动返回到生物识别和密码界面，显示有“关闭锁屏密码”的提示按钮')
        device.refresh_layout()
        device.assert_text_exist('更改锁屏密码')
        device.assert_text_exist('关闭锁屏密码')

        logging.info('步骤3.设备息屏后重新进入,输入错误密码')
        device.suspend()
        device.wakeup()
        device.unlock()
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_1)
        logging.info('预期结果3.需要输入锁屏密码才能进入，输入错误密码会提示“密码错误”')
        device.wait(1)
        device.assert_current_window(self.lock_screen.window_name)

        logging.info('步骤4.输入正确的锁屏密码')
        device.click(*self.lock_screen.num_1)
        device.click(*self.lock_screen.num_2)
        device.click(*self.lock_screen.num_3)
        device.click(*self.lock_screen.num_4)
        device.click(*self.lock_screen.num_5)
        device.click(*self.lock_screen.num_6)
        device.wait(2)
        logging.info('预期结果4.进入桌面')
        device.assert_current_window(self.app.window_name)
